<?php

namespace Modules\Asset\Http\Controllers\Admin\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Modules\Asset\Entities\AssetRecord;

use Validator;
use ThrowException;
use Auth;

class AssetRecordController extends Controller
{
    public function index(Request $req, $assetName)
    {
        $rule = [
        ];

        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        in_array($assetName, array_keys(config('asset.map'))) || ThrowException::Conflict('错误的资产类型');

        $options = $req->all();

        $options['with'] = ['user', 'admin'];

        $options['builder'] = AssetRecord::whereHas('user')->assetType($assetName);

        $options['only'] = [
            'id',
            'uuid',
            'before',
            'after',
            'value',
            'type',
            'sub_type',
            'order_number',
            'user' => [
                'id',
                'uuid',
                'name',
                'gender',
                'headimg',
                'phone'
            ],
            'admin' => [
                'name'
            ],
            'description',
            'created_at'
        ];

        $list = AssetRecord::getList($options);

        return $list->generateListResponse();
    }
}
